<!DOCTYPE html>
<html>
  <!-- meta/link... -->
  



<head>
  <meta http-equiv="content-type" content="text/html; charset=utf-8">
  <meta content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0" name="viewport">
  <!-- Global site tag (gtag.js) - Google Analytics -->


  <title>kubeadm安装Kubernetes，实践记录 | 张不胖</title>

  <link rel="icon" type="image/x-icon, image/vnd.microsoft.icon" href="/favicon.ico">
  <link rel="stylesheet" href="https://at.alicdn.com/t/font_1911880_c1nvbyezg17.css">
  <link href="https://cdn.jsdelivr.net/npm/font-awesome@4.7.0/css/font-awesome.min.css" rel="stylesheet">

  
    <script>
        var themeModelId = '';
        if (themeModelId) {
            localStorage.setItem('modelId', themeModelId);
        }
    </script>
    
    <script src="https://cdn.jsdelivr.net/gh/yuang01/live2d-widget@latest/autoload.js"></script>
    <script>
        var live2dOpen = eval('true') || false;
        if (!live2dOpen) {
            localStorage.setItem('waifu-display', 1609323474481);
        }
    </script>
  
  
  
<link rel="stylesheet" href="/css/animate.min.css">

  
<link rel="stylesheet" href="/css/style.css">

  
  
    
<link rel="stylesheet" href="/js/fancybox/jquery.fancybox.min.css">

  
  
    
<link rel="stylesheet" href="/js/shareJs/share.min.css">

  
  <style>
        @media (max-width: 992px) {
            #waifu {
                display: none;
            }
        }
    </style>
    <script src="//cdn.bootcss.com/pace/1.0.2/pace.min.js"></script>
    <link href="//cdn.bootcss.com/pace/1.0.2/themes/pink/pace-theme-flash.css" rel="stylesheet">

    

    <!-- import link -->
    
        
            
        
            
        
    
    <!-- import script -->
    
        
            <script src="/js/calendar.js"></script>
        
            <script src="/js/languages.js"></script>
        
            
        
            
        
    

<meta name="generator" content="Hexo 6.3.0"><link rel="alternate" href="/atom.xml" title="张不胖" type="application/atom+xml">
</head>

  
  <!-- 依赖于jquery和vue -->
  <script src="/js/jquery3.5.1.js"></script>
  <script src="/js/vue2.6.11.js"></script>
  
  <body>
    <!-- 预加载动画 -->
    <!-- 页面预加载动画 -->

    
    <!-- 判断是否为暗黑风格 -->
    <!-- 判断是否为黑夜模式 -->
<script>
  let isDark = JSON.parse(localStorage.getItem('dark')) || JSON.parse('false');

  if (isDark) {
    $(document.body).addClass('darkModel');
  }
</script>

    <!-- 头部导航等 -->
    
<header class="header " id="navHeader"
  style="position: fixed;
  left: 0; top: 0; z-index: 10;width: 100%;"
>
  <div class="header-content">
    <div class="bars">
      <div id="appDrawer" class="sidebar-image">
  <div class="drawer-box-icon">
    <i class="fa fa-bars" aria-hidden="true" @click="showDialogDrawer"></i>
  </div>
  
  <transition name="fade">
    <div class="drawer-box_mask" v-cloak style="display: none;" v-show="visible" @click.self="cancelDialogDrawer">
    </div>
  </transition>
  <div class="drawer-box" :class="{'active': visible}">
    <div class="drawer-box-head bg-color">
      <img class="drawer-box-head_logo" src="/medias/logo.png" alt="logo">
      <h3 class="drawer-box-head_title">张不胖</h3>
      <h5 class="drawer-box-head_desc"></h5>
    </div>
    
    <div class="drawer-box-content">
      <ul class="drawer-box-content_menu">
        
          
            <li class="drawer-box-content_item" style="position: relative;">
              
                <a href="/" class="drawer-menu-item-link">
                  
                    <i class="fa fa-home" aria-hidden="true"></i>
                  
                  <span class="name">首页</span>
                </a>
              
            </li>
          
            <li class="drawer-box-content_item" style="position: relative;">
              
                <a href="/musics" class="drawer-menu-item-link">
                  
                    <i class="fa fa-music" aria-hidden="true"></i>
                  
                  <span class="name">音乐</span>
                </a>
              
            </li>
          
            <li class="drawer-box-content_item" style="position: relative;">
              
                <a href="/archives" class="drawer-menu-item-link">
                  
                    <i class="fa fa-archive" aria-hidden="true"></i>
                  
                  <span class="name">归档</span>
                </a>
              
            </li>
          
            <li class="drawer-box-content_item" style="position: relative;">
              
                <a href="/tags" class="drawer-menu-item-link">
                  
                    <i class="fa fa-tags" aria-hidden="true"></i>
                  
                  <span class="name">标签</span>
                </a>
              
            </li>
          
            <li class="drawer-box-content_item" style="position: relative;">
              
                <a href="/categories" class="drawer-menu-item-link">
                  
                    <i class="fa fa-bookmark" aria-hidden="true"></i>
                  
                  <span class="name">分类</span>
                </a>
              
            </li>
          
            <li class="drawer-box-content_item" style="position: relative;">
              
                <a href="/about" class="drawer-menu-item-link">
                  
                    <i class="fa fa-user" aria-hidden="true"></i>
                  
                  <span class="name">关于</span>
                </a>
              
            </li>
          
            <li class="drawer-box-content_item" style="position: relative;">
              
                <a href="/friends" class="drawer-menu-item-link">
                  
                  <span class="name">友情链接</span>
                </a>
              
            </li>
          
            <li class="drawer-box-content_item" style="position: relative;">
              
                <a href="javascript:;" class="drawer-menu-item-link has-children" @click="openOrCloseMenu(7)">
                  <span>
                    
                      <i class="fa fa-link"></i>
                    
                    <span class="name">更多</span>
                  </span>
                  <i class="fa fa-chevron-left arrow " :class="{'icon-rotate': isOpen(7)}" aria-hidden="true"></i>
                </a>
                <ul class="drawer-sub-menu" v-if="isOpen(7)">
                  
                  <li>
                    <a href="/gallery">
                      
                      <i class="fa fa-music" style="margin-top: -20px;"></i>
                      
                      <span>图库</span>
                    </a>
                  </li>
                  
                  <li>
                    <a target="_blank" rel="noopener" href="http://baidu.com">
                      
                      <i class="fa fa-film" style="margin-top: -20px;"></i>
                      
                      <span>百度</span>
                    </a>
                  </li>
                  
                </ul>
              
            </li>
          
        
        
          <li class="drawer-box-content_item">
            <a target="_blank" rel="noopener" href="https://github.com/zhangcun1113/hexo-theme-bamboo">
              <i class="fa fa-github" aria-hidden="true"></i>
              <span>Github</span>
            </a>
          </li>
        
      </ul>
    </div>
  </div>
</div>

<script>
  var body = document.body || document.documentElement || window;
  var vm = new Vue({
    el: '#appDrawer',
    data: {
      visible: false,
      top: 0,
      openArr: [],
    },
    computed: {
    },
    mounted() {
    },
    methods: {
      isOpen(index) {
        if (this.openArr.includes(index)) {
          return true;
        } else {
          return false;
        }
      },
      openOrCloseMenu(curIndex) {
        const index = this.openArr.indexOf(curIndex);
        if (index !== -1) {
          this.openArr.splice(index, 1);
        } else {
          this.openArr.push(curIndex);
        }
      },
      showDialogDrawer() {
        this.visible = true;
        // 防止页面滚动，只能让弹框滚动
        this.top = $(document).scrollTop()
        body.style.cssText = 'width: 100%; height: 100%;overflow: hidden;';
      },
      cancelDialogDrawer() {
        this.visible = false;
        body.removeAttribute('style');
        $(document).scrollTop(this.top)
      }
    },
    created() {}
  })
</script>

    </div>
    <div class="blog-title" id="author-avatar">
      
        <div class="avatar">
          <img src="/medias/logo.png" alt="logo">
        </div>
      
      <a href="/" class="logo">张不胖</a>
    </div>
    <nav class="navbar">
      <ul class="menu">
        
          
            <li class="menu-item" style="position: relative;">
              
                <a href="/" class="menu-item-link" title="首页">
                  
                    <i class="fa fa-home" aria-hidden="true"></i>
                  
                  <span class="name">首页</span>
                </a>
              
            </li>
          
            <li class="menu-item" style="position: relative;">
              
                <a href="/musics" class="menu-item-link" title="音乐">
                  
                    <i class="fa fa-music" aria-hidden="true"></i>
                  
                  <span class="name">音乐</span>
                </a>
              
            </li>
          
            <li class="menu-item" style="position: relative;">
              
                <a href="/archives" class="menu-item-link" title="归档">
                  
                    <i class="fa fa-archive" aria-hidden="true"></i>
                  
                  <span class="name">归档</span>
                </a>
              
            </li>
          
            <li class="menu-item" style="position: relative;">
              
                <a href="/tags" class="menu-item-link" title="标签">
                  
                    <i class="fa fa-tags" aria-hidden="true"></i>
                  
                  <span class="name">标签</span>
                </a>
              
            </li>
          
            <li class="menu-item" style="position: relative;">
              
                <a href="/categories" class="menu-item-link" title="分类">
                  
                    <i class="fa fa-bookmark" aria-hidden="true"></i>
                  
                  <span class="name">分类</span>
                </a>
              
            </li>
          
            <li class="menu-item" style="position: relative;">
              
                <a href="/about" class="menu-item-link" title="关于">
                  
                    <i class="fa fa-user" aria-hidden="true"></i>
                  
                  <span class="name">关于</span>
                </a>
              
            </li>
          
            <li class="menu-item" style="position: relative;">
              
                <a href="/friends" class="menu-item-link" title="友情链接">
                  
                  <span class="name">友情链接</span>
                </a>
              
            </li>
          
            <li class="menu-item" style="position: relative;">
              
                <a href="javascript:;" class="menu-item-link" title="更多">
                  
                    <i class="fa fa-link"></i>
                  
                  <span class="name">更多</span>
                  <i class="fa fa-chevron-down arrow" aria-hidden="true"></i>
                </a>
                <ul class="sub-menu">
                  
                  <li>
                    <a href="/gallery">
                      
                      <i class="fa fa-music" style="margin-top: -20px;"></i>
                      
                      <span>图库</span>
                    </a>
                  </li>
                  
                  <li>
                    <a target="_blank" rel="noopener" href="http://baidu.com">
                      
                      <i class="fa fa-film" style="margin-top: -20px;"></i>
                      
                      <span>百度</span>
                    </a>
                  </li>
                  
                </ul>
              
            </li>
          
        
      </ul>
      
      
        <div id="appSearch">
  <div class="search"  @click="showDialog()"><i class="fa fa-search" aria-hidden="true"></i></div>
  <transition name="fade">
    <div class="message-box_wrapper" style="display: none;" v-cloak v-show="dialogVisible" @click.self="cancelDialogVisible()">
      <div class="message-box animated bounceInDown">
        <!-- <div class="close" style="overflow: hidden;">
          <i class="fa fa-close" aria-hidden="true" style="float:right;" @click.self="cancelDialogVisible()"></i>
        </div> -->
        <h2>
          <span>
            <i class="fa fa-search" aria-hidden="true"></i>
            <span class="title">本地搜索</span>
          </span>
          <i class="fa fa-close close" pointer style="float:right;" aria-hidden="true" @click.self="cancelDialogVisible()"></i>
        </h2>
        <form class="site-search-form">
          <input type="text"
            placeholder="请输入关键字"
            id="local-search-input" 
            @click="getSearchFile()"
            class="st-search-input"
            v-model="searchInput"
          />
        </form>
        <div class="result-wrapper">
          <div id="local-search-result" class="local-search-result-cls"></div>
        </div>
      </div>
    </div>
  </transition>
</div>
<script src="/js/local_search.js"></script>
<script>
  var body = document.body || document.documentElement || window;
  var vm = new Vue({
    el: '#appSearch',
    data: {
      dialogVisible: false,
      searchInput: '',
      top: 0,
    },
    computed: {
    },
    mounted() {
    },
    methods: {
      showDialog() {
        this.dialogVisible = true;
        // 防止页面滚动，只能让弹框滚动
        this.top = $(document).scrollTop()
        body.style.cssText = 'overflow: hidden;';
      },
      getSearchFile() {
        if (!this.searchInput) {
          getSearchFile("/search.xml");
        }
      },
      cancelDialogVisible() {
        this.dialogVisible = false;
        body.removeAttribute('style');
        $(document).scrollTop(this.top)
      },
    },
    created() {}
  })
</script>
<!-- 解决刷新页面闪烁问题，可以在元素上添加display: none, 或者用vue.extend方法，详情：https://blog.csdn.net/qq_31393401/article/details/81017912 -->
<!-- 下面是搜索基本写法 -->
<!-- <script type="text/javascript" id="local.search.active">
  var inputArea = document.querySelector("#local-search-input");
  inputArea.onclick   = function(){ getSearchFile(); this.onclick = null }
  inputArea.onkeydown = function(){ if(event.keyCode == 13) return false }
</script> -->

      

    </nav>
  </div>
  
    <a target="_blank" rel="noopener" href="https://github.com/zhangcun1113/hexo-theme-bamboo" class="github-corner color-primary" aria-label="View source on GitHub"><svg width="60" height="60" viewBox="0 0 250 250" style="fill:#fff; position: absolute; top: 0; border: 0; right: 0;" aria-hidden="true"><path d="M0,0 L115,115 L130,115 L142,142 L250,250 L250,0 Z"></path><path d="M128.3,109.0 C113.8,99.7 119.0,89.6 119.0,89.6 C122.0,82.7 120.5,78.6 120.5,78.6 C119.2,72.0 123.4,76.3 123.4,76.3 C127.3,80.9 125.5,87.3 125.5,87.3 C122.9,97.6 130.6,101.9 134.4,103.2" fill="currentColor" style="transform-origin: 130px 106px;" class="octo-arm"></path><path d="M115.0,115.0 C114.9,115.1 118.7,116.5 119.8,115.4 L133.7,101.6 C136.9,99.2 139.9,98.4 142.2,98.6 C133.8,88.0 127.5,74.4 143.8,58.0 C148.5,53.4 154.0,51.2 159.7,51.0 C160.3,49.4 163.2,43.6 171.4,40.1 C171.4,40.1 176.1,42.5 178.8,56.2 C183.1,58.6 187.2,61.8 190.9,65.4 C194.5,69.0 197.7,73.2 200.1,77.6 C213.8,80.2 216.3,84.9 216.3,84.9 C212.7,93.1 206.9,96.0 205.4,96.6 C205.1,102.4 203.0,107.8 198.3,112.5 C181.9,128.9 168.3,122.5 157.7,114.1 C157.9,116.9 156.7,120.9 152.7,124.9 L141.0,136.5 C139.8,137.7 141.6,141.9 141.8,141.8 Z" fill="currentColor" class="octo-body"></path></svg></a><style>.github-corner:hover .octo-arm{animation:octocat-wave 560ms ease-in-out}@keyframes octocat-wave{0%,100%{transform:rotate(0)}20%,60%{transform:rotate(-25deg)}40%,80%{transform:rotate(10deg)}}@media (max-width:500px){.github-corner:hover .octo-arm{animation:none}.github-corner .octo-arm{animation:octocat-wave 560ms ease-in-out}}</style>
  
  
    <div id="he-plugin-simple"></div>
    <script>
      WIDGET = {
        CONFIG: {
          "modules": "012",
          "background": 5,
          "tmpColor": "4A4A4A",
          "tmpSize": 16,
          "cityColor": "4A4A4A",
          "citySize": 16,
          "aqiSize": 16,
          "weatherIconSize": 24,
          "alertIconSize": 18,
          "padding": "10px 10px 10px 10px",
          "shadow": "1",
          "language": "auto",
          "borderRadius": 5,
          "fixed": "false",
          "vertical": "middle",
          "horizontal": "center",
          "key": "2784dd3fcb1e4f0f9a9b579bf69641f2"
        }
      }
    </script>
    <script src="https://widget.qweather.net/simple/static/js/he-simple-common.js?v=2.0"></script> 
    

    <!-- 当头部导航设置为背景透明的时候  -->
    
      <script src="/js/header/index.js" data-pjax></script>
    
</header>

    <!-- 需要在上面加载的js -->
    <script>
    function loadScript(src, cb) {
        setTimeout(function () {
            var HEAD = document.getElementsByTagName('head')[0] || document.documentElement;
            var script = document.createElement('script');
            script.setAttribute('type', 'text/javascript');
            if (cb) script.onload = cb;
            script.setAttribute('src', src);
            HEAD.appendChild(script);
        });
    }

    //https://github.com/filamentgroup/loadCSS
    var loadCSS = function (href, before, media, attributes) {
        var doc = window.document;
        var ss = doc.createElement("link");
        var ref;
        if (before) {
            ref = before;
        }
        else {
            var refs = (doc.body || doc.getElementsByTagName("head")[0]).childNodes;
            ref = refs[refs.length - 1];
        }
        var sheets = doc.styleSheets;
        if (attributes) {
            for (var attributeName in attributes) {
                if (attributes.hasOwnProperty(attributeName)) {
                    ss.setAttribute(attributeName, attributes[attributeName]);
                }
            }
        }
        ss.rel = "stylesheet";
        ss.href = href;
        ss.media = "only x";
        function ready(cb) {
            if (doc.body) {
                return cb();
            }
            setTimeout(function () {
                ready(cb);
            });
        }
        ready(function () {
            ref.parentNode.insertBefore(ss, (before ? ref : ref.nextSibling));
        });
        var onloadcssdefined = function (cb) {
            var resolvedHref = ss.href;
            var i = sheets.length;
            while (i--) {
                if (sheets[i].href === resolvedHref) {
                    return cb();
                }
            }
            setTimeout(function () {
                onloadcssdefined(cb);
            });
        };
        function loadCB() {
            if (ss.addEventListener) {
                ss.removeEventListener("load", loadCB);
            }
            ss.media = media || "all";
        }
        if (ss.addEventListener) {
            ss.addEventListener("load", loadCB);
        }
        ss.onloadcssdefined = onloadcssdefined;
        onloadcssdefined(loadCB);
        return ss;
    };
    
</script>
 

<!-- 轮播图所需要的js -->
<script src="/js/swiper/swiper.min.js"></script>
<script src="/js/swiper/vue-awesome-swiper.js"></script>
<script src="/js/swiper/swiper.animate1.0.3.min.js"></script>


  <script src="/js/vue-typed-js/index.js"></script>


<!-- 首页的公告滚动插件的js需要重新加载 -->
<script src="/js/vue-seamless-scroll/index.js"></script>

<!-- 打字机效果js -->
<script src="https://cdn.jsdelivr.net/npm/typed.js@2.0.11"></script>


    <!-- 内容区域 -->
    <div id="safearea">
      <main class="main" id="pjax-container" style="margin-top: 0;">
        
 <!-- prismjs 代码高亮 -->
 
    
    <link href="/js/prism/prism-line-numbers.css" rel="stylesheet">
    
        <link href="/js/prism/prism.min.css" rel="stylesheet">
    

    <style>
        pre[class*="language-"] {
            overflow-y: hidden;
        }
        .line-numbers .line-numbers-rows {
            border: none;
        }
    </style>
  


<div class="bg-dark-floor" style="position: fixed;left: 0;top: 0;width: 100%;height: 100%;z-index: -1;"></div>

<!-- 文章详情页顶部图片和标题 -->




<div class="post-detail-header lazyload" id="thumbnail_canvas" data-original="/medias/14.jpg" style="background-repeat: no-repeat; background-size: cover; 
  background-position: center center;position: relative;">
  <div class="post-detail-header-mask"></div>
  <canvas id="header_canvas"style="position:absolute;bottom:0;pointer-events:none;"></canvas>
  
  <div class="post-detail-header_info-box">
    <div class="title-box">
      <span class="title">
        kubeadm安装Kubernetes，实践记录
      </span>
    </div>
    
    
      
        <span class="post-detail-header_date">
          <i class="fa fa-calendar"></i> 发表于：2022-09-26 |
        </span>
      

      

      
        <div class="post-detail-header_wordcount">
          <span class="totalcount">
            <i class="fa fa-file-text-o"></i> 字数统计: 3.9k |
          </span>
  
          <span class="min2read">
            <i class="fa fa-clock-o"></i> 阅读时长: 18分钟 |
          </span>
  
          
            <span class="reading">
              <i class="fa fa-eye"></i> 阅读量：<span id="busuanzi_value_page_pv"></span>
            </span>
          
        </div>
      
    
  </div>
  
  
    <script src="/js/bubble/bubble.js"></script>
  
</div>



<div class="row justify-position">
  <div class="main-content">
    <article class="post post-detail">
      
      <div class="post-content">
        <pre><code>kubeadm安装Kubernetes，实践记录
这是一次成功的安装、胜利的安装，是值得收藏的一次记录。

本文主要介绍主控节点的安装，工作节点添加参见：

“Kubernetes加入新节点，经验总结“-https://my.oschina.net/u/2306127/blog/1816989
这里以1.9.3为例：

1.10.x版本安装参考：https://my.oschina.net/u/2306127/blog/1806263
国内1.10.2，https://www.cnblogs.com/RainingNight/archive/2018/05/02/8975838.html


赠人玫瑰，手有余香。本文来源-https://my.oschina.net/u/2306127/blog/1628082

环境：
主机使用MacOS+VirtualBox，虚拟机为Ubuntu Desktop 16.04.4，网络设为桥接。
kubernetes版本为1.9.3，docker-ce使用17.03.0(后来升级到docker-ce 18.02，也可以正常运行)。
注意：
kubeadm还在快速发展、演化之中，遇到问题可以查看官网的最新信息（https://kubernetes.io/docs/setup/independent/install-kubeadm/）
遇到问题，可以先搜索https://github.com/kubernetes/kubernetes/issues。
或者到 github.com/kubernetes 上反馈，提交issue，帮助改进。
shell脚本和工具下载，https://github.com/openthings/kubernetes-tools。
Kubernetes是一个强大的容器治理平台。不过对于一个可以管理大规模集群的系统，安装起来也并非易事。而且由于原始的Kubernetes相关Docker镜像和安装文件都托管在gcloud上，链接指向也都是无法访问的，因此手工操作是不可避免的。再加之Kubernetes本身也在快速的发展，各种坑和不同版本的差异让成功运行Kubernetes难度更大。

最简单的方式是使用minikube（参见：https://my.oschina.net/u/2306127/blog/1621468）或者Docker for Mac/Windows(参见：https://my.oschina.net/u/2306127/blog/1606599)，但这只能在开发环境下使用（用于桌面级服务也很有用），不支持多机集群和多节点的规模伸缩。

Kubernetes的Dockerhub镜像，https://hub.docker.com/r/mirrorgooglecontainers
Kubeadm的具体使用参考，https://my.oschina.net/u/2306127/blog/1627651
kubeadm是一个kubernetes的安装工具，可以用于快速部署kubernets集群，但上面的问题依然存在，我们可以将镜像从dockerhub上先拉下来，再改成需要的名称。在这之前，目前版本还需要有一些小的手工设置（目前版本Kubernetes基础服务已经全部容器化了，以后版本安装过程可能都全部自动化了），如下：

1、预先设置
由于目前版本兼容性限制，在Ubuntu上需要调整几个设置：

1.1 关闭swap
临时关闭，运行：swapoff -a。下次启动还有。
到/etc/fstab中永久删除或关闭swap分区，使用 # 注释掉即可。
1.2 关闭和清理ufw
下面的命令将清除现有的所有防火墙规则：

iptables -F
1.3 设置cgroups
确保kubelet使用的cgroup driver 与 Docker的一致。要么使用下面的方法更新 Docker：

cat &lt;&lt; EOF &gt; /etc/docker/daemon.json
&#123;
  &quot;exec-opts&quot;: [&quot;native.cgroupdriver=systemd&quot;]
&#125;
EOF
要么，设置kubernetes的cgroup driver，如：kubelet 的 --cgroup-driver 标志设置为与 Docker 一样(e.g. cgroupfs)。

1.4 安装docker 17.03
目前的Kubernetes 1.9.3验证的docker版本最高为CE 17.03，使用下面的方法安装：

apt-get update
apt-get install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | apt-key add -
add-apt-repository \
   &quot;deb https://download.docker.com/linux/$(. /etc/os-release; echo &quot;$ID&quot;) \
   $(lsb_release -cs) \
   stable&quot;
apt-get update &amp;&amp; apt-get install -y docker-ce=$(apt-cache madison docker-ce | grep 17.03 | head -1 | awk &#39;&#123;print $3&#125;&#39;)
apt-cache madison docker-ce可以查看可用的版本。
！apt-get代理的单独设置。

Apt-get有时候无法访问，设了系统代理也不行。可以尝试单独设置apt的代理，如下：
$ nano /etc/apt/apt.conf
Acquire::http::Proxy &quot;http://192.168.199.99:9999&quot;;
Acquire::https::Proxy &quot;http://192.168.199.99:9999&quot;;
或者在执行时加上-E参数（指示apt使用环境变量），如下所示（参考：https://askubuntu.com/questions/257290/configure-proxy-for-apt）：
sudo -E https_proxy=192.168.199.99:9999 apt install docker-ce=17.03.2~ce-0~ubuntu-xenial
不要更新，否则docker会被更新到最新的版本。可以尝试最新版本，是可以运行的。
！参照如下格式设置docker的独立代理，清除所有系统代理（因为Kubernetes要访问本地服务）。

编辑docker服务的配置文件，然后重新启动/etc/systemd/system/docker.service。
详细说明 https://my.oschina.net/u/2306127/blog/1627620 。
运行 sudo gedit /lib/systemd/system/docker.service，加入设置如下：
Environment=&quot;HTTP_PROXY=http://192.168.199.99:9999/&quot; 
Environment=&quot;HTTPS_PROXY=http://192.168.199.99:9999/&quot; 
Environment=&quot;NO_PROXY=localhost,127.0.0.0/8&quot;
但是，在安装时还是遇到奇慢无比的情况，只能多尝试一下。

2、拉取镜像
Kubernetes的原始docker镜像在gcloud上，即使使用代理也需要注册、登录，使用gcloud工具才能获取，使用docker会导致莫名其妙的失败（出错信息含糊）。可以从DockerHub上面拉取Kubernetes镜像的复制品，然后修改docker的名称（以后可以通过kubeadm的配置文件来安装，应该就可以指定安装源了）。

2.1 创建自动拉取脚本
脚本如下，如果需要其它的容器镜像可以照此增加即可，可以将版本号修改为自己需要的。

注意：kubernetes每个版本依赖的版本不同，下面适用1.9.3。
echo &quot;==================================================&quot;
echo &quot;Set proxy to http://192.168.199.99:9999...&quot;
echo &quot;&quot;
export http_proxy=http://192.168.199.99:9999
export https_proxy=http://192.168.199.99:9999
echo &quot;==================================================&quot;
 
echo &quot;&quot;
echo &quot;Pulling Docker Images from mirrorgooglecontainers...&quot;
 
echo &quot;==&gt;kube-apiserver:&quot;
docker pull mirrorgooglecontainers/kube-apiserver-amd64:v1.9.3
docker tag mirrorgooglecontainers/kube-apiserver-amd64:v1.9.3 gcr.io/google_containers/kube-apiserver-amd64:v1.9.3
 
echo &quot;==&gt;kube-controller-manager:&quot;
docker pull mirrorgooglecontainers/kube-controller-manager-amd64:v1.9.3
docker tag mirrorgooglecontainers/kube-controller-manager-amd64:v1.9.3 gcr.io/google_containers/kube-controller-manager-amd64:v1.9.3
 
echo &quot;==&gt;kube-scheduler:&quot;
docker pull mirrorgooglecontainers/kube-scheduler-amd64:v1.9.3 
docker tag mirrorgooglecontainers/kube-scheduler-amd64:v1.9.3 gcr.io/google_containers/kube-scheduler-amd64:v1.9.3
 
echo &quot;==&gt;kube-proxy:&quot;
docker pull mirrorgooglecontainers/kube-proxy-amd64:v1.9.3
docker tag mirrorgooglecontainers/kube-proxy-amd64:v1.9.3 gcr.io/google_containers/kube-proxy-amd64:v1.9.3
 
echo &quot;==&gt;k8s-dns-sidecar:&quot;
docker pull mirrorgooglecontainers/k8s-dns-sidecar-amd64:1.14.8
docker tag mirrorgooglecontainers/k8s-dns-sidecar-amd64:1.14.8 gcr.io/google_containers/k8s-dns-sidecar-amd64:1.14.8
 
echo &quot;==&gt;k8s-dns-kube-dns:&quot;
docker pull mirrorgooglecontainers/k8s-dns-kube-dns-amd64:1.14.8
docker tag mirrorgooglecontainers/k8s-dns-kube-dns-amd64:1.14.8 gcr.io/google_containers/k8s-dns-kube-dns-amd64:1.14.8
 
echo &quot;==&gt;k8s-dns-dnsmasq-nanny:&quot;
docker pull mirrorgooglecontainers/k8s-dns-dnsmasq-nanny-amd64:1.14.8
docker tag mirrorgooglecontainers/k8s-dns-dnsmasq-nanny-amd64:1.14.8 gcr.io/google_containers/k8s-dns-dnsmasq-nanny-amd64:1.14.8
 
echo &quot;==&gt;etcd:&quot;
docker pull mirrorgooglecontainers/etcd-amd64:3.1.11
docker tag mirrorgooglecontainers/etcd-amd64:3.1.11 gcr.io/google_containers/etcd-amd64:3.1.11
 
echo &quot;==&gt;pause:&quot;
docker pull mirrorgooglecontainers/pause-amd64:3.0
docker tag mirrorgooglecontainers/pause-amd64:3.0 gcr.io/google_containers/pause-amd64:3.0
 
echo finished.
echo &quot;More update, please visit: https://hub.docker.com/r/mirrorgooglecontainers&quot;
echo &quot;&quot;

2.2 运行镜像获取脚本
建议将上面内容保存到getkubeimage.sh，然后运行即可。如下：

gedit getkubeimages.sh
#复制、黏贴上面的脚本内容。
sudo chmod +x getkubeimages.sh
sudo ./getkubeimages.sh
3、Kubeadm安装
3.1 首先安装kubeadm
kubeadm是用于kubernetes安装、维护的命令行工具。

echo &quot;添加Kubernetes安装源认证key:&quot;
sudo curl -sSL https://packages.cloud.google.com/apt/doc/apt-key.gpg | apt-key add
 
echo &quot;添加Kubernetes安装源：&quot;
sudo echo “deb http://apt.kubernetes.io/ kubernetes-xenial main” &gt; /etc/apt/sources.list.d/kubernetes.list
 
echo &quot;更新系统软件包列表：&quot;
sudo apt update
 
echo &quot;查看Kubernetes的可用版本：&quot;
apt-cache madison kubeadm
 
echo &quot;安装kubeadm 1.9.3: &quot;
apt-get install -y kubeadm=1.9.3-00
！！！修正kubelet启动参数的错误。
对于kubeadm 1.9.3版本。将下面的内容添加到 /etc/systemd/system/kubelet.service.d/10-kubeadm.conf 文件。

[Service]
Environment=&quot;KUBELET_CGROUP_ARGS=--cgroup-driver=systemd&quot;
Environment=&quot;KUBELET_EXTRA_ARGS=--fail-swap-on=false&quot;
以及将 KUBELET_CGROUP_ARGS 加到启动参数中（该参数在本版本安装文件中丢失）。

最后 /etc/systemd/system/kubelet.service.d/10-kubeadm.conf 文件如下：

[Service]
 
Environment=&quot;KUBELET_KUBECONFIG_ARGS=--bootstrap-kubeconfig=/etc/kubernetes/bootstrap-kubelet.conf --kubeconfig=/etc/kubernetes/kubelet.conf&quot;
Environment=&quot;KUBELET_SYSTEM_PODS_ARGS=--pod-manifest-path=/etc/kubernetes/manifests --allow-privileged=true&quot;
Environment=&quot;KUBELET_NETWORK_ARGS=--network-plugin=cni --cni-conf-dir=/etc/cni/net.d --cni-bin-dir=/opt/cni/bin&quot;
Environment=&quot;KUBELET_DNS_ARGS=--cluster-dns=10.96.0.10 --cluster-domain=cluster.local&quot;
Environment=&quot;KUBELET_AUTHZ_ARGS=--authorization-mode=Webhook --client-ca-file=/etc/kubernetes/pki/ca.crt&quot;
Environment=&quot;KUBELET_CADVISOR_ARGS=--cadvisor-port=0&quot;
Environment=&quot;KUBELET_CERTIFICATE_ARGS=--rotate-certificates=true --cert-dir=/var/lib/kubelet/pki&quot;
 
Environment=&quot;KUBELET_CGROUP_ARGS=--cgroup-driver=systemd&quot;
Environment=&quot;KUBELET_EXTRA_ARGS=--fail-swap-on=false&quot;
 
ExecStart=
ExecStart=/usr/bin/kubelet $KUBELET_KUBECONFIG_ARGS $KUBELET_SYSTEM_PODS_ARGS $KUBELET_NETWORK_ARGS $KUBELET_DNS_ARGS $KUBELET_AUTHZ_ARGS $KUBELET_CADVISOR_ARGS $KUBELET_CERTIFICATE_ARGS $KUBELET_CGROUP_ARGS $KUBELET_EXTRA_ARGS

！然后重启kubelet服务，运行：
systemctl daemon-reload
systemctl restart kubelet
注意：在每一个节点参照上面修改kubelet的服务配置文件，否则kubelet启动失败。
可以使用 sudo systemctl status kubelet查看kubelet服务的状态。
3.2 运行kubeadm init 来启动集群创建
kubeadm init --kubernetes-version=v1.9.3 --pod-network-cidr=192.168.0.0/16
或者：

kubeadm init --kubernetes-version=v1.9.3 –pod-network-cidr 10.244.0.0/16
Using CoreDNS, add:

kubeadm init --kubernetes-version=v1.9.3 –pod-network-cidr 10.244.0.0/16 --feature-gates CoreDNS=true
使用Kubeadm安装的具体命令参考 https://my.oschina.net/u/2306127/blog/1627651。

添加节点
记住上面返回值的添加节点命令，到其他节点上执行即可。
如果忘记了，重新获取上面的命令，运行：
 kubeadm token create --print-join-command --ttl 0
可以使用kubeadm token -h 查看token操作。
重新生成CA授权码：
#openssl x509 -pubkey -in /etc/kubernetes/pki/ca.crt | openssl rsa -pubin -outform der 2&gt;/dev/null | openssl dgst -sha256 -hex | sed &#39;s/^.* //&#39;
参见讨论：https://github.com/kubernetes/kubeadm/issues/26    
3.3 安装flannel网络驱动
按照上面完成后的提示执行： 

sudo kubectl apply -f https://raw.githubusercontent.com/coreos/flannel/master/Documentation/kube-flannel.yml
！！！安装flannel网络出现pending挂起问题（podCidr参数丢失，即便在kubeadm运行时已指定）：
#查看日志：
kubectl get pods --namespace kube-system
kubectl --namespace kube-system logs kube-flannel-ds-bvxd2
#上面的kube-flannel-ds-bvxd2为flannel的pod名称，根据get pods返回的名称输入。
 
#编辑 /etc/kubernetes/manifests/kube-controller-manager.yaml
在command节，加入：
- --allocate-node-cidrs=true
- --cluster-cidr=10.244.0.0/16
 
然后system restart kubelet即可。
该问题解决办法的详细参考：https://github.com/coreos/flannel/issues/728

3.4 主控节点开启
默认设置主控节点不开启任务节点功能，可以通过命令打开，从而将主控和任务节点部署到一台机器上。

kubectl taint nodes --all node-role.kubernetes.io/master-
3.5 加入集群节点
使用kubeadm join命令将其它任务节点加入主控集群。

kubeadm join –token 8dc9d8.df09161bed020a12 192.168.199.106:6443 –discovery-token-ca-cert-hash sha256:16exxx
3.6 安装失败的恢复
如果安装失败，使用 kubeadm reset 重置安装环境。大部分时候需要重启操作系统再行运行kubeadm init，因为一些系统网络服务端口已经被占用了，而且无法简单地清除。

注意：如果改变了代理设置，需要将控制台窗口关闭，然后再重新打开。否则，Ubuntu系统的“设置”-&gt;“网络”-&gt;“代理”设置对话框改变的是全局环境变量http_proxy和https_proxy没有清除，仍然是有效的，导致本地服务连接不上。这也是一些服务如apt/node.js/github（代理设置参考 https://my.oschina.net/u/2306127/blog/869727）没有使用这些环境变量，导致虽然进行了操作系统设置但是仍然无效的原因所在。
4、Kubernetes安装成功信息
Kubernetes安装成功，输出信息如下所示：

root@kube:/home/supermap# kubeadm init --kubernetes-version v1.9.3 --pod-network-cidr=10.244.0.0/16[init] Using Kubernetes version: v1.9.3
[init] Using Authorization modes: [Node RBAC]
[preflight] Running pre-flight checks.
    [WARNING FileExisting-crictl]: crictl not found in system path
[preflight] Starting the kubelet service
[certificates] Generated ca certificate and key.
[certificates] Generated apiserver certificate and key.
[certificates] apiserver serving cert is signed for DNS names [kube kubernetes kubernetes.default kubernetes.default.svc kubernetes.default.svc.cluster.local] and IPs [10.96.0.1 192.168.199.111]
[certificates] Generated apiserver-kubelet-client certificate and key.
[certificates] Generated sa key and public key.
[certificates] Generated front-proxy-ca certificate and key.
[certificates] Generated front-proxy-client certificate and key.
[certificates] Valid certificates and keys now exist in &quot;/etc/kubernetes/pki&quot;
[kubeconfig] Wrote KubeConfig file to disk: &quot;admin.conf&quot;
[kubeconfig] Wrote KubeConfig file to disk: &quot;kubelet.conf&quot;
[kubeconfig] Wrote KubeConfig file to disk: &quot;controller-manager.conf&quot;
[kubeconfig] Wrote KubeConfig file to disk: &quot;scheduler.conf&quot;
[controlplane] Wrote Static Pod manifest for component kube-apiserver to &quot;/etc/kubernetes/manifests/kube-apiserver.yaml&quot;
[controlplane] Wrote Static Pod manifest for component kube-controller-manager to &quot;/etc/kubernetes/manifests/kube-controller-manager.yaml&quot;
[controlplane] Wrote Static Pod manifest for component kube-scheduler to &quot;/etc/kubernetes/manifests/kube-scheduler.yaml&quot;
[etcd] Wrote Static Pod manifest for a local etcd instance to &quot;/etc/kubernetes/manifests/etcd.yaml&quot;
[init] Waiting for the kubelet to boot up the control plane as Static Pods from directory &quot;/etc/kubernetes/manifests&quot;.
[init] This might take a minute or longer if the control plane images have to be pulled.
[apiclient] All control plane components are healthy after 33.501916 seconds
[uploadconfig] Storing the configuration used in ConfigMap &quot;kubeadm-config&quot; in the &quot;kube-system&quot; Namespace
[markmaster] Will mark node kube as master by adding a label and a taint
[markmaster] Master kube tainted and labelled with key/value: node-role.kubernetes.io/master=&quot;&quot;
[bootstraptoken] Using token: 8b2ed3.149a349e4b775985
[bootstraptoken] Configured RBAC rules to allow Node Bootstrap tokens to post CSRs in order for nodes to get long term certificate credentials
[bootstraptoken] Configured RBAC rules to allow the csrapprover controller automatically approve CSRs from a Node Bootstrap Token
[bootstraptoken] Configured RBAC rules to allow certificate rotation for all node client certificates in the cluster
[bootstraptoken] Creating the &quot;cluster-info&quot; ConfigMap in the &quot;kube-public&quot; namespace
[addons] Applied essential addon: kube-dns
[addons] Applied essential addon: kube-proxy
 
Your Kubernetes master has initialized successfully!
 
To start using your cluster, you need to run the following as a regular user:
 
  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config
 
You should now deploy a pod network to the cluster.
Run &quot;kubectl apply -f [podnetwork].yaml&quot; with one of the options listed at:
  https://kubernetes.io/docs/concepts/cluster-administration/addons/
 
You can now join any number of machines by running the following on each node
as root:
 
  kubeadm join --token 8b2ed3.149a349e4b775985 192.168.199.111:6443 --discovery-token-ca-cert-hash sha256:ab69621f2117f2b283df725859724efc71c37a20f6da519237ca1dad5a72d9b2

后续操作，执行：

  mkdir -p $HOME/.kube
  sudo cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
  sudo chown $(id -u):$(id -g) $HOME/.kube/config
然后运行 kubectl get ns 获取命名空间（namespaces）的信息，输出如下：

supermap@kube:~$ kubectl get ns
NAME          STATUS    AGE
default       Active    22m
kube-public   Active    22m
kube-system   Active    22m
把本机作为任务节点加入：

kubectl taint nodes --all node-role.kubernetes.io/master-
查看节点情况：

supermap@kube:~$ kubectl get node
NAME      STATUS     ROLES     AGE       VERSION
kube      NotReady   master    28m       v1.9.3
现在，本机同时运行了主控和一个任务节点。
查看节点详细情况：

#显示pods列表。
kubectl get pods --namespace=kube-system -o wide
 
#得到pod的详细信息，其中kube-dns-6f4fd4bdf-895jh为节点名称。
kubectl get -o json --namespace=kube-system pods/kube-dns-6f4fd4bdf-895jh 
5、安装dashboard和helm
后续安装dns、dashboard、helm时还需要下面几个镜像，架上代理拉下来，或者到hub.docker.com上找到pull下来，在docker tag成相应的名字，否则kubectl get pods会显示一直处于pending状态。

docker pull quay.io/coreos/flannel:v0.10.0-amd64
docker pull gcr.io/google_containers/k8s-dns-kube-dns-amd64:1.14.7
docker pull k8s.gcr.io/kubernetes-dashboard-amd64:v1.8.3
docker pull gcr.io/kubernetes-helm/tiller:v2.8.1
参考：

Kubernetes Dashboard面板服务安装，https://my.oschina.net/u/2306127/blog/1606599
dashboard安装授权登录详细参考，https://my.oschina.net/u/2306127/blog/1630861
Helm软件包管理服务安装，https://my.oschina.net/u/2306127/blog/1619818
映射dashboard的端口，让外面可以访问：

kubectl port-forward kubernetes-dashboard-7798c48646-wkgk4 8443:8443 --namespace=kube-system &amp;
注意，kubernetes-dashboard-7798c48646-wkgk4是kubectl get pods --namespace=kube-system -o wide列出的dashboard的pod名称。
dashboard在使用kubeadm安装的系统上授权还有些问题，详细参考：https://kubernetes.io/docs/tasks/access-application-cluster/web-ui-dashboard/#deploying-the-dashboard-ui
现在，可以打开浏览器，输入 http://localhost:8443查看kubernetes的运行情况。
</code></pre>

      </div>
      <div class="post-tags-categories">
        
      </div>
      
        <div class="copyright">
  <ul class="post-copyright">
    <li class="post-copyright-author">
    <strong>作者:  </strong>张存</a>
    </li>
    <li class="post-copyright-link">
    <strong>文章链接:  </strong>
    <a href="/2022/09/26/kubeadm-an-zhuang-kubernetes-shi-jian-ji-lu/" target="_blank" title="kubeadm安装Kubernetes，实践记录">https://blog.zhangcun.store/2022/09/26/kubeadm-an-zhuang-kubernetes-shi-jian-ji-lu/</a>
    </li>
    <li class="post-copyright-license">
      <strong>版权声明:   </strong>
      本网站所有文章除特别声明外,均采用 <a rel="license" href="https://creativecommons.org/licenses/by-nc-nd/4.0/" target="_blank" title="Attribution-NonCommercial-NoDerivatives 4.0 International (CC BY-NC-ND 4.0)">CC BY-NC-ND 4.0</a>
      许可协议。转载请注明出处!
    </li>
  </ul>
<div>
      
    </article>
    <!-- 上一篇文章和下一篇文章 -->
    
      <!-- 文章详情页的上一页和下一页 -->
<div class="post-nav">



  
  <div class="post-nav-prev post-nav-item">
    <div class="post-nav-img lazyload" data-original="/medias/6.jpg" style="background-size: cover; 
      background-position: center center;">
    </div>
    <a href="/2022/09/26/mysql-cha-kan-dang-qian-yong-hu/" class="post-nav-link">
      <div class="title">
        <i class="fa fa-angle-left"></i> 上一篇:
        <div class="title-text">mysql 查看当前用户</div>
      </div>
      
      <!-- <div class="content">
         在Mysql中，可以通过如下方式查询Mysql数据库当前连接的用户及会话信息：
1. show full proces
      </div> -->
    </a>
  </div>



  
  <div class="post-nav-next post-nav-item">
    <div class="post-nav-img lazyload" data-original="/medias/6.jpg" style="background-size: cover; 
      background-position: center center;">
    </div>
    <a href="/2022/09/26/zai-docker-zhong-shi-yong-mysql-gao-ke-yong-zhi-mha/" class="post-nav-link">
      <div class="title">
        下一篇: <i class="fa fa-angle-right"></i>
        <div class="title-text">在Docker中使用MySQL高可用之MHA</div>
      </div>
      <!-- <div class="content">
        目录    一、MHA简介和架构    1.1 MHA简介    1.2 MHA工具包的组成    1.3 MHA架构 
      </div> -->
    </a>
  </div>

</div>

    
    

    <!-- 打赏 -->
    
      <div id="appDonate" class="post-donate">
  <div id="donate_board" class="donate_bar center" ref="donate">
    <a id="btn_donate" class="btn_donate" href="javascript:;" title="打赏" @click="showDialogDrawer()"></a>
  </div>
  <transition name="fade">
    <div 
      class="donate-box-mask"
      v-cloak 
      v-if="visible"
      @click="cancelDialogDrawer()"
    >
    </div>
  </transition>
  <transition name="bounce">
    <div class="donate-box" v-cloak v-if="visible">
      <div class="donate-box_close">
        <i class="fa fa-times" aria-hidden="true" @click="cancelDialogDrawer" pointer></i>
      </div>
      <div class="donate-box_title">
        <h4>
          你的赏识是我前进的动力
        </h4>
      </div>
      <div class="donate-box_tab">
        <div class="Alipay" pointer :class="{'active': tabActive === 'Alipay'}" @click="changeTabActive('Alipay')">
          支付宝
        </div>
        <div class="WeChatpay" pointer :class="{'active': tabActive === 'WeChatpay'}" @click="changeTabActive('WeChatpay')">
          微信
        </div>
      </div>
      <div class="donate-box_img">
        <div class="AlipayImg" v-if="tabActive === 'Alipay'">
          <img src="/medias/zfb.jpg" alt="支付宝打赏" />
        </div> 
        <div class="WeChatpayImg" v-if="tabActive === 'WeChatpay'">
          <img src="/medias/wx.jpg" alt="微信打赏" />
        </div>
      </div>
    </div>
  </transition>
</div>

<script>
  var body = document.body || document.documentElement || window;
  var vm = new Vue({
    el: '#appDonate',
    data: {
      visible: false,
      tabActive: 'Alipay',
      top: 0,
    },
    computed: {
    },
    mounted() {
    },
    methods: {
      showDialogDrawer() {
        this.visible = true;
        // 防止页面滚动，只能让弹框滚动
        // function getScroll() {
        //   return {
        //     left: window.pageXOffset || document.documentElement.scrollLeft || document.body.scrollLeft || 0,
        //     top: window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0
        //   };
        // }
        this.top = $(document).scrollTop() // or getScroll().top
        // console.log('aa', $('.main-content'));
        body.style.cssText = 'overflow: hidden;';
      },
      cancelDialogDrawer() {
        this.visible = false;
        body.removeAttribute('style');
        $(document).scrollTop(this.top)
      },
      changeTabActive(name) {
        this.tabActive = name;
      }
    },
    created() {}
  })
</script>
    

    <!-- 分享 -->
    
      <!-- https://github.com/overtrue/share.js -->
<!-- 文章详情页的分享 -->
<div class="social-share" data-sites="twitter,facebook,google,qq,qzone,wechat,weibo,douban,linkedin" data-wechat-qrcode-helper="<p>微信扫一扫即可分享！</p>"></div>

<script src="/js/shareJs/social-share.min.js"></script>
</script>

<style>
  .social-share {
    margin: 20px 0;
  }
</style>


    
    
    <!-- 评论 -->
    <!-- 评论 -->

  <div id="myComment">
    
      <div id="gitment-container"></div>

    
  </div>

<!-- comment script in themes\hexo-theme-bamboo\layout\_partial\scripts\index.ejs -->


  </div>

  <!-- 目录 -->
  <!-- 文章详情页右侧目录 -->

  <div class="toc-aside">
    <div class="toc-main">
      <div class="toc-aside-title">
        <i class="fa fa-list-ul" aria-hidden="true"></i><span>本文目录</span>
        
          <div class="toc-open-close">本文目录</div>
        
      </div>
      <div class="toc-content">
        <div class="toc"></div>
      </div>
    </div>
  </div>
  <!-- js在scripts目录下的的toc.ejs里面 -->
  

  <script>
    function openBtnClickFn () {
      let openOrCloseBtn = $('.toc-aside .toc-aside-title .toc-open-close');
      let open = eval('' || 'true');
      openOrCloseBtn.click(function() {
        if (open === true) {
          $(".toc-aside").css({'width': 0, 'padding': 0});
          $(".toc-content").css({'width': 0});
          $(".toc-aside-title span, .toc-aside-title i").css({'display': 'none'});
          $(".main-content").css({'width': '75%', 'margin': '10px auto'});
          open = false;
        } else {
          $(".toc-aside").css({'width': '300px', 'padding': '0 10px'});
          $(".toc-content").css({'width': '300px'});
          $(".toc-aside-title span, .toc-aside-title i").css({'display': 'inline-block'});
          $(".main-content").css({'width': '65%', 'margin-right': '10px', 'margin-left': 'calc(35% - 350px)'});
          open = true;
        }
      });
    };
    openBtnClickFn();
    document.addEventListener('pjax:complete', function () {
      openBtnClickFn();
    })
  </script>


  

  <!-- 图片放大 Wrap images with fancybox support -->
  <script src="/js/wrapImage.js"></script>
</div>

<!-- 文章详情页背景图 -->
<div id="appBgSwiper" style="position: fixed;left: 0;top: 0;width: 100%;height: 100%;z-index: -2;" :style="{'background-color': bgColor ? bgColor : 'transparent'}">
    <transition-group tag="ul" :name="names">
        <li v-for='(image,index) in img' :key='index' v-show="index === mark" class="bg-swiper-box">
            <img :src="image" class="bg-swiper-img">
        </li>
    </transition-group>
</div>
<script>
    var vm = new Vue({
        el: '#appBgSwiper',
        data: {
            names: '' || 'fade' || 'fade', // translate-fade fade
            mark: 0,
            img: [],
            bgColor: '',
            time: null
        },
        methods: {   //添加方法
            change(i, m) {
                if (i > m) {
                    // this.names = 'fade';
                } else if (i < m) {
                    // this.names = 'fade';
                } else {
                    return;
                }
                this.mark = i;
            },
            prev() {
                // this.names = 'fade';
                this.mark--;
                if (this.mark === -1) {
                    this.mark = 3;
                    return
                }
            },
            next() {
                // this.names = 'fade';
                this.mark++;
                if (this.mark === this.img.length) {
                    this.mark = 0;
                    return
                }
            },
            autoPlay() {
                // this.names = 'fade';
                this.mark++;
                if (this.mark === this.img.length) {
                    this.mark = 0;
                    return
                }
            },
            play() {
                let bgImgDelay = '' || '180000'
                let delay = parseInt(bgImgDelay) || 180000;
                this.time = setInterval(this.autoPlay, delay);
            },
            enter() {
                clearInterval(this.time);
            },
            leave() {
                this.play();
            }
        },
        created() {
            this.play()
        },
        beforeDestroy() {
            clearInterval(this.time);
        },
        mounted() {
            let prop = ''|| '';
            let isImg = prop.includes('.bmp') || prop.includes('.jpg') || prop.includes('.png') || prop.includes('.tif') || prop.includes('.gif') || prop.includes('.pcx') || prop.includes('.tga') || prop.includes('.exif') || prop.includes('.fpx') || prop.includes('.psd') || prop.includes('.cdr') || prop.includes('.pcd') || prop.includes('.dxf') || prop.includes('.ufo') || prop.includes('.eps') || prop.includes('.ai') || prop.includes('.raw') || prop.includes('.WMF') || prop.includes('.webp') || prop.includes('.jpeg') || prop.includes('http://') || prop.includes('https://') 
            if (isImg) {
                let img = prop.split(',');
                let configRoot = '/'
                let arrImg = [];
                img.forEach(el => {
                    var Expression=/http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/;
			        var objExp = new RegExp(Expression);

                    if(objExp.test(el)) {
                        // http or https
                        arrImg.push(el);
                    } else {
                        // 非http or https开头
                        // 本地文件
                        let firstStr = el.charAt(0);
                        if (firstStr == '/') {
                            el = el.substr(1); // 删除第一个字符 '/',因为 configRoot最后一个字符为 /
                        }
                        el = configRoot + el;
                        arrImg.push(el);
                    }
                })
                this.img = arrImg;
            } else {
                this.bgColor = prop;
            }
        }
    })
</script>

<style>
    .bg-swiper-box {
        position: absolute;
        display: block;
        width: 100%;
        height: 100%;
    }
    .bg-swiper-img {
        object-fit: cover;
        width: 100%;
        height: 100%;
    }
</style>

      </main>
    </div>

    <!-- 页脚 -->
    
  
  <div class="footer bg-color">
    <div class="footer-main">
      
        
          <div class="link">
            
              
                <a href="/atom.xml" class="social">
                  
                    <i class="fa fa-rss" aria-hidden="true"></i>
                  
                </a>
              
            
              
                <a href="mailto:1776803208@qq.com" class="social">
                  
                    <i class="fa fa-envelope" aria-hidden="true"></i>
                  
                </a>
              
            
              
                <a target="_blank" rel="noopener" href="https://github.com/zhangcun1113/hexo-theme-bamboo" class="social">
                  
                    <i class="fa fa-github" aria-hidden="true"></i>
                  
                </a>
              
            
              
                <a href="tencent://AddContact/?fromId=50&amp;fromSubId=1&amp;subcmd=all&amp;uin=1730241541" class="social">
                  
                    <i class="fa fa-qq" aria-hidden="true"></i>
                  
                </a>
              
            
          </div>
        
      
        
          <div class="footer-copyright">
            <p>Copyright © 2001 - 2023 <a href="https://blog.zhangcun.store">zhangcun</a> </p>

          </div>
        
      
        
          
            <!-- 不蒜子统计 -->
            <!-- 不蒜子统计 -->
<span id="busuanzi_container_site_pv">
      <i class="fa fa-eye" aria-hidden="true"></i>本站总访问量：<span id="busuanzi_value_site_pv"></span> 次
</span>
<span class="post-meta-divider">|</span>
<span id="busuanzi_container_site_uv" style='display:none'>
      <i class="fa fa-users" aria-hidden="true"></i>本站访客数：<span id="busuanzi_value_site_uv"></span> 人
</span>

          
        
      
        
          <div class="footer-custom">
            
          </div>
        
      
    </div>
  </div>



    <!-- 渲染暗黑按钮 -->
    
      <div class="dark">
  <div class="dark-content">
    <i class="fa fa-moon-o" aria-hidden="true"></i>
    <!-- <span>关灯</span> -->
  </div>
  
</div>

<script>
  $(function() {
    let isDark = JSON.parse(localStorage.getItem('dark'))  || JSON.parse('false');
    if (isDark) {
      $(".dark-content").replaceWith(
          `
          <div class='dark-content'>
            <i class="fa fa-lightbulb-o" aria-hidden="true"></i>
          </div>
          `
        );
    }
    $('.dark').click(function() {
      if ($(document.body).is('.darkModel')) {
        $(document.body).removeClass('darkModel');
        localStorage.setItem('dark', false);
        $(".dark-content").replaceWith(
          `
          <div class='dark-content'>
            <i class="fa fa-moon-o" aria-hidden="true"></i>
          </div>
          `
        );
      } else {
        $(document.body).addClass('darkModel');
        localStorage.setItem('dark', true);
        $(".dark-content").replaceWith(
          `
          <div class='dark-content'>
            <i class="fa fa-lightbulb-o" aria-hidden="true"></i>
          </div>
          `
        );
      }
    })
  })
</script>
    
    <!-- 渲染回到顶部按钮 -->
    
      <div class="goTop top-btn-color" pointer>
  <i class="fa fa-arrow-up" aria-hidden="true"></i>
</div>
<script src="/js/goTop.js"></script>
    
    <!-- 渲染左下角音乐播放器 -->
    
      <link rel="stylesheet" href="/js/aplayer/APlayer@1.10.1.min.css">
<style>
.aplayer .aplayer-lrc p {
  
  font-size: 12px;
  font-weight: 700;
  line-height: 16px !important;
}

.aplayer .aplayer-lrc p.aplayer-lrc-current {
  
  font-size: 15px;
  color: #22b1eb;
}


.aplayer.aplayer-fixed.aplayer-narrow .aplayer-body {
  left: -66px !important;
}

.aplayer.aplayer-fixed.aplayer-narrow .aplayer-body:hover {
  left: 0px !important;
}


</style>
<meting-js  
  class=""
  server="netease"
  type="playlist"
  id="8444991407"
  fixed='true'
  autoplay='true'
  theme='#22b1eb'
  loop='all'
  order='random'
  preload='auto'
  volume='0.7'
  list-folded='true'
>
</meting-js>

<!-- <style>
  #aplayer {
    position: fixed;
    left: 0;
    bottom: 300px;
  }
</style> -->
<script src="https://cdn.jsdelivr.net/npm/aplayer@1.10.1/dist/APlayer.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/meting@2/dist/Meting.min.js"></script>
    

    <!-- 图片放大 -->
    
      <script src="/js/fancybox/jquery.fancybox.min.js"></script>
    

    <!-- 百度解析 -->
    <!-- Baidu Analytics -->

    <!-- Baidu Push -->

<script>
    (function () {
        var bp = document.createElement('script');
        var curProtocol = window.location.protocol.split(':')[0];
        if (curProtocol === 'https') {
            bp.src = 'https://zz.bdstatic.com/linksubmit/push.js';
        } else {
            bp.src = 'http://push.zhanzhang.baidu.com/push.js';
        }
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(bp, s);
    })();
</script>

    
    <!-- 背景彩带 -->
    
    
    <script src="/js/app.js" data-pjax></script>
    
    <!-- 文章目录所需js -->
<link href="/js/tocbot/tocbot.css" rel="stylesheet">
<script src="/js/tocbot/tocbot.min.js"></script>

<script>
  var headerEl = 'h2, h3, h4',  //headers 
      content = '.post-detail',//文章容器
      idArr = {};  //标题数组以确定是否增加索引id
      //add #id
  var option = {
    // Where to render the table of contents.
    tocSelector: '.toc',
    // Where to grab the headings to build the table of contents.
    contentSelector: content,
    // Which headings to grab inside of the contentSelector element.
    headingSelector: headerEl,
    scrollSmooth: true,
    scrollSmoothOffset: -80,
    headingsOffset: -($(window).height() * 0.4 - 45),
    positionFixedSelector: '.toc-main',
    positionFixedClass: 'is-position-fixed',
    fixedSidebarOffset: 'auto',
  }
  if ($('.toc').length > 0) {

    $(content).children(headerEl).each(function () {
      //去除空格以及多余标点
      var headerId = $(this).text().replace(/[\s|\~|`|\!|\@|\#|\$|\%|\^|\&|\*|\(|\)|\_|\+|\=|\||\|\[|\]|\{|\}|\;|\:|\"|\'|\,|\<|\.|\>|\/|\?|\：|\，|\。]/g, '');

      headerId = headerId.toLowerCase();
      if (idArr[headerId]) {
        //id已经存在
        $(this).attr('id', headerId + '-' + idArr[headerId]);
        idArr[headerId]++;
      }
      else {
        //id未存在
        idArr[headerId] = 1;
        $(this).attr('id', headerId);
      }
    });

    tocbot.init(option);
    
  }


  document.addEventListener('pjax:send', function () {
    if ($('.toc').length) {
      tocbot.destroy();
    }
  });

  document.addEventListener('pjax:complete', function () {
    if ($('.toc').length) {
      tocbot.init(option);
    }
  });

  
</script>

<style>
.is-position-fixed {
  position:sticky !important;
  top: 74px;
}
.toc-main ul {
  counter-reset: show-list;
}
.toc-main ul li::before {
  content: counter(item)".";
  display: block;
  position: absolute;
  left: 12px;
  top:0;
}
/* .toc > .toc-list {
  padding-left: 35px;
}
.toc>.toc-list li {
  list-style: decimal;
}
.toc-list {
  padding-left: 25px;
} */
</style> 

<!-- 渲染issues标签里的内容 -->
<script>
  function loadIssuesJS() {
    if ($(".post-detail").find(".issues-api").length == 0) {
      return;
    } 
    loadScript('/js/issues/index.js');
  };
  $(function () {
    loadIssuesJS();
  });
  document.addEventListener('pjax:complete', function () {
    if (typeof IssuesAPI == "undefined") {
      loadIssuesJS();
    }
  })
</script>

<!-- 输入框打字特效 -->
<!-- 输入框打字特效 -->

  <script src="/js/activate-power-mode.js"></script>
  <script>
    POWERMODE.colorful = true;  // 打开随机颜色特效
    POWERMODE.shake = false;    // 关闭输入框抖动
    document.body.addEventListener('input', POWERMODE);//监听打字事件
  </script>


<!-- markdown代码一键复制功能 -->

  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/v-plugs-ayu/lib/ayu.css">
  <script src="https://cdn.jsdelivr.net/npm/v-plugs-ayu/lib/ayu.umd.min.js"></script>
  <script src="/js/clipboard/clipboard.min.js"></script>
  <div id="appCopy">
  </div>
  <script data-pjax>
    var vm = new Vue({
      el: '#appCopy',
      data: {
      },
      computed: {
      },
      mounted() {
        const that = this;
        var copy = '复制';
        /* code */
        var initCopyCode = function () {
          var copyHtml = '';
          copyHtml += '<button class="btn-copy" data-clipboard-snippet="" style="position:absolute;top:0;right:0;z-index:1;">';
          copyHtml += '<i class="fa fa-copy"></i><span>' + copy + '</span>';
          copyHtml += '</button>';
          $(".post-detail pre").not('.gutter pre').wrap("<div class='codeBox' style='position:relative;width:100%;'></div>")
          $(".post-detail pre").not('.gutter pre').before(copyHtml);
          new ClipboardJS('.btn-copy', {
            target: function (trigger) {
              return trigger.nextElementSibling;
            }
          });
        }
        initCopyCode();
        $('.btn-copy').unbind('click').bind('click', function () {
          doSomething();
        })
        $(document).unbind('keypress').bind('keypress', function (e) {
          if (e.ctrlKey && e.keyCode == 67) {
            doSomething();
            // 返回false, 防止重复触发copy事件  
            // return false;
          }
        })
        //  监听 ctrl + c事件  
        // $(document).unbind('keypress').bind('keypress', function (e) {
        //   if (e.ctrlKey && e.keyCode == 67) {
        //     doSomething();
        //     // 返回false, 防止重复触发copy事件  
        //     return false;
        //   }
        // })

        // // 鼠标右键的复制事件  
        // $(document).unbind('copy').bind('copy', function (e) {
        //   doSomething();
        // });

        function doSomething() {
          that.$notify({
            title: "成功",
            content: "代码已复制，请遵守相关授权协议。",
            type: 'success'
          })
        }
      },
      methods: {
      },
      created() { }
    })
  </script>


<!-- 图片懒加载 -->
<script src="/js/lazyload/lazyload@1.9.1.js"></script>
<script data-pjax>
  $(function () {
    $("div.lazyload, img.lazyload, a.lazyload").lazyload(
      {
        effect: "show",
        placeholder: "https://img10.360buyimg.com/ddimg/jfs/t1/159345/8/2271/222193/5ff7b36fEe49f5663/a95287c20385127f.gif"    //图片未加载出来时显示的占位图
      }
    );
  });
</script>

<!-- 卡片滚动动画 -->
   

<!-- 评论所需js -->

    
        <script type="text/javascript">
    var bamboo = {};
    bamboo.utterance = {};
    
    function check_utterance() {
        let isDark = JSON.parse(localStorage.getItem('dark'))  || JSON.parse('false');
        if (isDark) {
            bamboo.utterance.Theme = 'github-dark';
        } else {
            bamboo.utterance.Theme = 'github-light';
        }

        return document.getElementById("gitment-container");
    }
    comment_el = '#gitment-container';
    load_utterance = function () {
        if ($(comment_el).length) {
            // 匿名函数，防止污染全局变量
            const HEAD = check_utterance();
            
            var utterances = document.createElement('script');
            utterances.type = 'text/javascript';
            utterances.async = true;
            utterances.setAttribute('issue-term','pathname')
            utterances.setAttribute('theme', bamboo.utterance.Theme)
            utterances.setAttribute('repo','')
            utterances.crossorigin = 'anonymous';
            utterances.src = 'https://utteranc.es/client.js';
            // content 是要插入评论的地方
            document.getElementById('gitment-container').appendChild(utterances);
            
        }
    }

    function dark_utterance() {
        const HEAD = check_utterance();
        if (!HEAD) return;
        const message = {
            type: 'set-theme',
            theme: bamboo.utterance.Theme
        };
        const utteranceIframe = document.querySelector('iframe');
        utteranceIframe.contentWindow.postMessage(message, 'https://utteranc.es');
    }

    $(document).ready(load_utterance);
    document.addEventListener('pjax:complete', function () {
        load_utterance();
    });

    $('.dark').click(function() {
        setTimeout(() => {
            dark_utterance();
        });
    })
    
</script>

<style>
    .utterances {
        max-width: inherit!important;
    }
</style>
    


<!-- 鼠标点击特效 -->
<!-- 爱心点击 -->

	
		<canvas class="fireworks" style="position: fixed;left: 0;top: 0;z-index: 999; pointer-events: none;" ></canvas>
		<script src="//cdn.bootcss.com/animejs/2.2.0/anime.min.js"></script>
		<script src="/js/cursor/explosion.min.js"></script>
	




    <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" data-pjax></script>


    <!-- pjax -->
    

<!-- pjax -->


  <script src="/js/pjax@0.2.8/index.js"></script>
  
    <!-- 样式位于：source/css/_third-party/pjaxanimate.styl -->

<div class="pjax-animate">
  
    <div class="loading-circle"><div id="loader-circle"></div></div>
    <script>
      window.ShowLoading = function() {
        $(".loading-circle").css("display", "block");
      };
      window.HideLoading = function() {
        $(".loading-circle").css("display", "none");
      }
    </script>
  
	<script>
    document.addEventListener('pjax:complete', function () {
      window.HideLoading();
    })
    document.addEventListener('pjax:send', function () {
      window.ShowLoading();
    })
    document.addEventListener('pjax:error', function () {
      window.HideLoading();
    })
	</script>
</div>

  

  <script>
    var pjax = new Pjax({
      elements: 'a[href]:not([href^="#"]):not([href="javascript:void(0)"]):not([no-pjax])',   // 拦截正常带链接的 a 标签
      selectors: ["#pjax-container","title"],                                   // 根据实际需要确认重载区域
      cacheBust: false,   // url 地址追加时间戳，用以避免浏览器缓存
      timeout: 5000
    });

    document.addEventListener('pjax:send', function (e) {

      try {
        var currentUrl = window.location.pathname;
        var targetUrl = e.triggerElement.href;
        var banUrl = [""];
        if (banUrl[0] != "") {
          banUrl.forEach(item => {
            if(currentUrl.indexOf(item) != -1 || targetUrl.indexOf(item) != -1) {
              window.location.href = targetUrl;
            }
          });
        }
      } catch (error) {}

      $(window).unbind('resize');
      $(window).unbind('scroll');
      $(document).unbind('scroll');
      $(document).unbind('click');
      $('body').unbind('click');

    })
    
    document.addEventListener('pjax:complete', function () {
      $('script[data-pjax], .pjax-reload script').each(function () {
        $(this).parent().append($(this).remove());
      });
    });

    document.addEventListener('pjax:error', function (e) {
      window.location.href = e.triggerElement.href;
    })
    
    // 刷新不从顶部开始
    document.addEventListener("DOMContentLoaded", function () {
      history.scrollRestoration = 'auto';
    })
  </script>



  </body>
</html>